package common

import "math"

type Pagination[T any] struct {
	Page       int   `json:"page" form:"page"`
	PageSize   int   `json:"pageSize" form:"pageSize"`
	Total      int64 `json:"total"`
	TotalPages int   `json:"totalPages"`
	Data       []T   `json:"data"`
}

func NewPagination[T any](page, pageSize int, total int64, data []T) *Pagination[T] {
	if page < 1 {
		page = 1
	}
	if pageSize < 1 {
		pageSize = 10 // 默认每页10条
	}

	totalPages := int(math.Ceil(float64(total) / float64(pageSize)))

	return &Pagination[T]{
		Page:       page,
		PageSize:   pageSize,
		Total:      total,
		TotalPages: totalPages,
		Data:       data,
	}
}
